/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package charts;

import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jfree.chart.JFreeChart;

/**
 *
 * @author Memet
 */
public class JPEG {

    /**
     * Saves a chart to a file
     * @param chart the chart that has to be saved
     * @param aFileName the filename where the file should be saved
     * @param width the prefered width of the image
     * @param height the prefered height of the image
     * @param quality the prefered quality of the image
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static void saveToFile(JFreeChart chart,
            String aFileName,
            int width,
            int height,
            double quality)
            throws FileNotFoundException, IOException {
        BufferedImage img = draw(chart, width, height);

        FileOutputStream fos = new FileOutputStream(aFileName);
//        JPEGImageEncoder encoder2 =
//                JPEGCodec.createJPEGEncoder(fos);
//        JPEGEncodeParam param2 =
//                encoder2.getDefaultJPEGEncodeParam(img);
//        param2.setQuality((float) quality, true);
//        encoder2.encode(img, param2);
        fos.close();
    } // saveToFile

    /**
     * Draws the image
     * @param chart the chart that has to be drawn
     * @param width the prefered width of the image
     * @param height the prefered height of the image
     * @return the image
     */
    protected static BufferedImage draw(JFreeChart chart, int width, int height) {
        BufferedImage img =
                new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = img.createGraphics();


        chart.draw(g2, new Rectangle2D.Double(0, 0, width, height));

        g2.dispose();
        return img;
    } // end draw
} // end class JPEG